//============================================================================
// Name        : MCCproof.cpp
// Author      : Angelo
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>

#include "Graph.h"

using namespace std;

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	int minWidth, maxWidth, maxGrado, zeroPerc;
	int repetitions;

	//initPar
	minWidth = 1;
	maxWidth = 25;
	maxGrado = 4;
	zeroPerc = 0.6;
	repetitions = 6000;

	srand (time(NULL));

	for (int nn = 5; nn <= 20; nn++) {

		std::vector<double> vecMinW (nn,0);
		std::vector<double> vecMeanW (nn,0);
		std::vector<double> vecSumW (nn,0);
		std::vector<double> vecCountW (nn,0);

		cout << "Starting with " << nn << " nodes." << endl;

		cout << "Repetition: ";
		for(int rr = 0; rr < repetitions; rr++) {
			Graph* randomG = new Graph(minWidth, maxWidth, maxGrado, nn, zeroPerc);

			cout << rr << " ";
			fflush(stdout);

			double graphW = randomG->sumwidth;

			for (int budget = 1; budget <= nn; budget++) {
				int w = randomG->getBestSubGraph(budget);

				//cout << "Graph width: " << graphW << "; Max with " << budget << ": " << w << endl;

				double wPerc = ((double) w) / graphW;

				if ((vecMinW[budget-1] == 0) || (wPerc < vecMinW[budget-1])) {
					vecMinW[budget-1] = wPerc;
				}

				vecSumW[budget-1] += wPerc;
				vecCountW[budget-1] += 1;
			}
		}

		cout << endl;

		for (int i = 0; i < nn; i++) {
			if (vecCountW[i] > 0) {
				vecMeanW[i] = vecSumW[i] / vecCountW[i];
			}
		}

		FILE *fileStatMin, *fileStatMinOK, *fileStatMean;
		char buff[128];
		char str_buff[64];
		int nBuff;

		snprintf(str_buff, sizeof(str_buff), "StatMin_%d.dat", nn);
		fileStatMin = fopen(str_buff, "w");
		if (fileStatMin) {

			for (int j = 0; j < nn; j++) {
				nBuff = snprintf(buff, sizeof(buff), "%d\t\t%lf\r\n", j+1, vecMinW[j]);
				fwrite(buff, 1, nBuff, fileStatMin);
			}

			fclose(fileStatMin);
		}

		snprintf(str_buff, sizeof(str_buff), "StatMinOK_%d.dat", nn);
		fileStatMinOK = fopen(str_buff, "w");
		if (fileStatMinOK) {

			for (int j = 0; j < nn; j++) {
				double xVal = ((double) (j+1)) / ((double) nn);
				nBuff = snprintf(buff, sizeof(buff), "%.4f\t\t%lf\r\n", xVal, vecMinW[j]);
				fwrite(buff, 1, nBuff, fileStatMinOK);
			}

			fclose(fileStatMinOK);
		}

		snprintf(str_buff, sizeof(str_buff), "StatMean_%d.dat", nn);
		fileStatMean = fopen(str_buff, "w");
		if (fileStatMean) {

			for (int j = 0; j < nn; j++) {
				nBuff = snprintf(buff, sizeof(buff), "%d\t\t%lf\r\n", j+1, vecMeanW[j]);
				fwrite(buff, 1, nBuff, fileStatMean);
			}

			fclose(fileStatMean);
		}

		/*cout << "Statistics (MIN) for " << nn << " nodes:" << endl;
		for (int j = 0; j < nn; j++) {
			cout << vecMinW[j] << " - ";
		}
		cout << endl;

		cout << "Statistics (MEAN) for " << nn << " nodes:" << endl;
		for (int j = 0; j < nn; j++) {
			cout << vecMeanW[j] << " - ";
		}
		cout << endl;
		cout << endl;*/

		cout << "End with " << nn << " nodes." << endl;

	}


	cout << "!!!END World!!!" << endl; // prints !!!Hello World!!!

	return 0;
}
